package com.cskaoyan.service.impl;

import com.cskaoyan.bean.bo.WxFootDeleteBo;
import com.cskaoyan.bean.bo.WxFootprintListBo;
import com.cskaoyan.bean.pojo.*;
import com.cskaoyan.bean.vo.WxFootPrintVo;
import com.cskaoyan.bean.vo.WxFootPrintVo1;
import com.cskaoyan.mapper.MallFootprintMapper;
import com.cskaoyan.mapper.MallGoodsMapper;
import com.cskaoyan.service.WxFootPrintService;
import com.github.pagehelper.PageHelper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @Author: chaohe@com.cskaoyan
 * @Date: 2021/7/12
 * @Time: 14:12
 */
@Service
public class WxFootPrintServiceImpl implements WxFootPrintService {
    @Autowired
    MallFootprintMapper mallFootprintMapper;
    @Autowired
    MallGoodsMapper mallGoodsMapper;

    @Override
    public WxFootPrintVo list(WxFootprintListBo wxFootprintListBo) {

        PageHelper.startPage(wxFootprintListBo.getPage(), wxFootprintListBo.getSize());

        MallFootprintExample mallFootprintExample = new MallFootprintExample();
        MallFootprintExample.Criteria criteria = mallFootprintExample.createCriteria();



        Subject subject = SecurityUtils.getSubject();
        MallUser mallUser = (MallUser) subject.getPrincipal();
        Integer id = mallUser.getId();

        criteria.andUserIdEqualTo(id);
        criteria.andDeletedEqualTo(0);

        mallFootprintExample.setOrderByClause("add_time" + " " + "desc");
        List<MallFootprint> mallFootprint = mallFootprintMapper.selectByExample(mallFootprintExample);
        WxFootPrintVo wxFootPrintVo = new WxFootPrintVo();
        List<WxFootPrintVo1> wxFootPrintVo1List = new ArrayList<>();

        for (MallFootprint footprint : mallFootprint) {


            MallGoods mallGoods = mallGoodsMapper.selectByPrimaryKey(footprint.getGoodsId());

            WxFootPrintVo1 wxFootPrintVo1 = new WxFootPrintVo1(mallGoods.getBrief(), mallGoods.getPicUrl(), footprint.getAddTime(), footprint.getGoodsId(), mallGoods.getName(), footprint.getId(), mallGoods.getRetailPrice());

            wxFootPrintVo1List.add(wxFootPrintVo1);

        }
        wxFootPrintVo.setFootprintList(wxFootPrintVo1List);

        return wxFootPrintVo;

    }

    @Override
    public void delete(WxFootDeleteBo wxFootDeleteBo) {

        MallFootprintExample mallFootprintExample = new MallFootprintExample();
        MallFootprintExample.Criteria criteria = mallFootprintExample.createCriteria();

        MallFootprint mallFootprint = mallFootprintMapper.selectByPrimaryKey(wxFootDeleteBo.getId());

        criteria.andIdEqualTo(wxFootDeleteBo.getId());
        MallFootprint mallFootprint1 = new MallFootprint(mallFootprint.getId(),mallFootprint.getUserId(),mallFootprint.getGoodsId(),mallFootprint.getAddTime(),mallFootprint.getUpdateTime(),true);


        int i = mallFootprintMapper.updateByExample(mallFootprint1, mallFootprintExample);
        if (i == 1) {
            return;
        }



    }
}










